package DroidEye.DAO;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import DroidEye.HelloApplication;
import DroidEye.DAO.DAOInterface.JudgeDAOInterface;
import DroidEye.Dto.Apply;
import DroidEye.Dto.Judge;
import DroidEye.JavaBean.JudgeBean;
import DroidEye.Mapper.ApplyMapper;
import DroidEye.Mapper.JudgeMapper;

@Service
public class JudgeDAO implements JudgeDAOInterface {
    @Autowired
    private JudgeMapper judgeMapper;
    
    @Autowired
    private ApplyMapper applyMapper;

    @Override
    public boolean setAdju(JudgeBean judgeBean) {
        Judge record = new Judge();
        record.setStudentId(judgeBean.getStudentID());
        record.setProjectId(judgeBean.getProjectID());
        record.setAdjuId(judgeBean.getAdjuID());
        
        int num = judgeMapper.insertSelective(record);
        if(num == 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean isRepetitive(String adjuID, String studentID, String project_id) {
        Judge judge = new Judge();
        judge.setAdjuId(adjuID);
        judge.setStudentId(studentID);
        judge.setProjectId(project_id);
        int number = judgeMapper.queryCountByCondition(judge);
        if(number == 1) {
            return true;
        }
        return false;
    }

    @Override
    public List<JudgeBean> queryAllNonAdju(String adjuID) {
        List<JudgeBean> unJudgeList = new ArrayList<JudgeBean>();
        Judge param = new Judge();
        param.setIsJudged("未评分");
        param.setAdjuId(adjuID);
        List<Judge> judgeList = judgeMapper.queryAllNonAdju(param);
        if(CollectionUtils.isEmpty(judgeList)) {
            return unJudgeList;
        }
        return custJudgeList(judgeList);
    }

    @Override
    public boolean addScore(String studentID, String projectID, String adjuID, String newScore,int hasScoredAdjuNumber) {
        updateProjectScoreByCondition(studentID, projectID, adjuID, newScore);
        hasScoredAdjuNumber = hasScoredAdjuNumber + 1;
        updateAdjuNumber(studentID, hasScoredAdjuNumber);
        updateIsJudged(studentID, projectID, adjuID);
        return true;
    }
    
    // 获取评审次数
    public Integer getHasScoredAdjuNumber(Apply param) {
        List<Apply> applys = applyMapper.queryApplyById(param);
        if(CollectionUtils.isEmpty(applys)) {
            return 0;
        }
        
        Apply apply = applys.get(0);
        String hasScoredAdjuNumber = apply.getHasScoredAdjuNumber();
        if(StringUtils.isEmpty(hasScoredAdjuNumber)) {
            return 0;
        }
        
        Integer result = null;
        try {
            result = Integer.parseInt(hasScoredAdjuNumber);
        }catch(Exception e) {
            e.printStackTrace();
        }
        
        if(result == null) {
            result = 0;
        }
        return result;
    }
    
    private void updateIsJudged(String studentID, String projectID, String adjuID) {
        Judge judgeParam = new Judge();
        judgeParam.setStudentId(studentID);
        judgeParam.setProjectId(projectID);
        judgeParam.setAdjuId(adjuID);
        judgeMapper.updateIsJudged(judgeParam);
    }
    
    private void updateProjectScoreByCondition(String studentID, String projectID, String adjuID, String newScore) {
        Judge judgeParam = new Judge();
        judgeParam.setProjectScore(newScore);
        judgeParam.setStudentId(studentID);
        judgeParam.setProjectId(projectID);
        judgeParam.setAdjuId(adjuID);
        judgeMapper.updateProjectScoreByCondition(judgeParam);
    }
    
    private void updateAdjuNumber(String studentID, int hasScoredAdjuNumber) {
        Apply param = new Apply();
        param.setStudentId(studentID);
        param.setHasScoredAdjuNumber(hasScoredAdjuNumber + "");
        applyMapper.updateAdjuNumber(param);
    }

    @Override
    public boolean updateTotalScore(String studentID, String totalScore) {
        Apply param = new Apply();
        param.setProjectTotalScore(totalScore);
        param.setStudentId(studentID);
        int num = applyMapper.updateTotalScore(param);
        if(num == 0) {
            return false;
        }
        return true;
    }
    
    // 转换JudgeList为JudgeBeanList
    private List<JudgeBean> custJudgeList(List<Judge> judges) {
        List<JudgeBean> judgeList = new ArrayList<>();
        for(Judge judge : judges) {
            JudgeBean judgeBean = custJudge(judge);
            judgeList.add(judgeBean);
        }
        return judgeList;
    }
    
    // 转换Judge为JudgeBean
    private JudgeBean custJudge(Judge judge) {
        JudgeBean judgeBean = new JudgeBean();
        judgeBean.setAdjuID(judge.getAdjuId());
        judgeBean.setStudentID(judge.getStudentId());
        judgeBean.setProjectID(judge.getProjectId());
        judgeBean.setProjectScore(judge.getProjectScore());
        return judgeBean;
    }
    
    public static JudgeDAO getJudgeDAOInstance() {
        return HelloApplication.getBean("judgeDAO", JudgeDAO.class);
    }
}
